package dialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import main.Game;
import model.Bank;
import model.Board;
import model.Cell;
import model.City;
import view.Card_view;

public class SellPropertyDialog extends JDialog{
	Game game;
	Bank bank;
	Font font = new Font("Arial",Font.BOLD,35);
	JPanel player, property, offer, confirm, card;
	JLabel error;
	JButton sell, cancel;
	JTextField offer_answer; 
	City c;
	JComboBox<String>players,properties;
	DefaultListCellRenderer centr = new DefaultListCellRenderer();



	public SellPropertyDialog(JFrame parent, String title, boolean modal, Game g,Bank bank){
		super(parent,title,modal);
		this.setSize(800,800);
		this.getContentPane().setLayout(new GridBagLayout());
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.game=g;
		this.bank=bank;
		initComponent();
		this.setVisible(true);
	}

	private void initComponent(){

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(7, 0, 7, 0);
		gbc.gridx=0;
		gbc.gridy=0;
		JLabel title = new JLabel("Sell Property");
		title.setFont(font);
		this.add(title,gbc);

		centr.setHorizontalAlignment(JLabel.CENTER);
		gbc.gridy=1;
		player = new JPanel();
		player.setPreferredSize(new Dimension(700,70));
		player.setBorder(BorderFactory.createTitledBorder("Player"));//panel where you will choose the player for the trade.
		font = new Font("Arial",Font.BOLD,15);
		players = new JComboBox<>();
		players.setRenderer(centr);
		players.setFont(font);
		player.setAlignmentX(CENTER_ALIGNMENT);
		//players.setPreferredSize(new Dimension(300,50));
		for(int i=0;i<game.listPlayer.size();i++){
			if(!game.listPlayer.get(i).equals(game.actual_player)){
				players.addItem(game.listPlayer.get(i).getName());
			}
		}
		player.add(players);//we add the combobox of players to the panel 
		this.add(player,gbc);

		gbc.gridy=2;
		property=new JPanel();
		property.setLayout(new GridBagLayout());
		GridBagConstraints gb = new GridBagConstraints();
		property.setPreferredSize(new Dimension(700,350));
		property.setBorder(BorderFactory.createTitledBorder("Property"));
		properties=new JComboBox<String>();
		properties.setRenderer(centr);
		properties.setPreferredSize(new Dimension(200,50));
		properties.setFont(font);
		for(int i=0;i<game.actual_player.getAllCards().size();i++){
			properties.addItem(game.actual_player.getAllCards().get(i).name);
		}
		card = new JPanel();
		card.setBorder(BorderFactory.createTitledBorder("Card"));
		card.setPreferredSize(new Dimension(230,300));
		for(int i=0;i<game.actual_player.getAllCards().size();i++){
			if(properties.getItemAt(0).equals((game.actual_player.getAllCards().get(i)).name)){
				c=(City) game.actual_player.getAllCards().get(i);
				card.add(new Card_view(c));
			}
		}
		gb.gridx=0;
		gb.gridy=0;

		property.add(properties,gb);
		gb.insets = new Insets(0, 50, 0, 0);
		gb.gridx=1;
		property.add(card,gb);
		properties.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox<String>item = (JComboBox<String>) e.getSource();
				for(int i=0;i<game.actual_player.getAllCards().size();i++){
					if(item.getSelectedItem()!=null){
						if(item.getSelectedItem().equals((game.actual_player.getAllCards().get(i)).name)){
							card.removeAll();
							c=(City) game.actual_player.getAllCards().get(i);
							card.add(new Card_view(c));
							card.repaint();card.revalidate();
						}
					}
				}

			}
		});


		this.add(property,gbc);

		gbc.gridy=3;
		offer=new JPanel();
		offer.setLayout(new BoxLayout(offer,BoxLayout.Y_AXIS));
		error = new JLabel("");
		offer.setPreferredSize(new Dimension(700,100));
		offer.setBorder(BorderFactory.createTitledBorder("Offer"));
		offer_answer =new JTextField(12);
		offer_answer.setMaximumSize(new Dimension(300,25));
		offer.add(error);
		offer.add(Box.createRigidArea(new Dimension(0,10)));
		offer.add(offer_answer);
		offer_answer.setAlignmentX(CENTER_ALIGNMENT);
		offer_answer.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		error.setAlignmentX(CENTER_ALIGNMENT);
		this.add(offer,gbc);

		gbc.gridy=4;
		confirm=new JPanel();
		confirm.setPreferredSize(new Dimension(700,100));

		sell=new JButton("sell");
		//sell listener
		sell.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Cell cell=null;
				for(int i=0;i<Board.map.length;i++){
					if(Board.map[i].getName().equals(c.name)){
						cell=Board.map[i];
					}
				}
				//player can't sell a place with houses on it
				if(cell.getNbHouses()>0){
					JOptionPane.showMessageDialog(null,"You can't have any house on your selling place");
					dispose();
					game.buyHouses();
				}
				else{
					if(offer_answer.getText().length()==0){
						error.setText("You must enter something");
						offer_answer.setBorder(BorderFactory.createLineBorder(Color.red));
					}
					else {
						try{
							int entier = Integer.parseInt(offer_answer.getText());
							if(entier>0){

								offer_answer.setBorder(BorderFactory.createLineBorder(Color.black));
								Object [] value = {game.actual_player, offer_answer.getText(),properties.getSelectedItem()};
								String playerName=players.getSelectedItem().toString();
								if(game.deal.containsKey(playerName)){
									game.deal.get(playerName).add(value);
								}
								else{
									ArrayList<Object[]>list = new ArrayList<Object[]>();
									list.add(value);
									game.deal.put(playerName ,list);
								}
								game.wantSellProperty=true;
								dispose();
							}
							else {
								error.setText("offer must be positive");
								offer_answer.setBorder(BorderFactory.createLineBorder(Color.red));
							}
						}catch(NumberFormatException e){error.setText("offer must be a positive integer");offer_answer.setBorder(BorderFactory.createLineBorder(Color.red));}
					}
				}
			}
		});

		cancel=new JButton("cancel");
		//cancel listener
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		confirm.add(sell);
		confirm.add(cancel);
		this.add(confirm,gbc);

	}
}
